﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PersonProject;
using Microsoft.Win32;
using System.IO;

namespace WPFClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        PersonService.Service1Client client = new PersonService.Service1Client();
        Person EditingPerson = null;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Btn_Load_Click(object sender, RoutedEventArgs e)
        {
            DataGrid_Persons.ItemsSource = client.Load();
            //DataGrid_Persons.IsManipulationEnabled = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            EditPanel.Visibility = System.Windows.Visibility.Hidden;
        }

        private void DataGrid_Persons_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EditingPerson = (Person)DataGrid_Persons.SelectedCells[0].Item;
            FillEditPanel();
            EditPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void Btn_Create_Click(object sender, RoutedEventArgs e)
        {
            EditingPerson = new Person();
            FillEditPanel();
            EditPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void Btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            Person PersonToDelete = (Person)DataGrid_Persons.SelectedCells[0].Item;
            client.Delete(PersonToDelete.Id);
            Btn_Load_Click(this, new RoutedEventArgs());
        }

        private void Btn_ok_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;

            switch (btn.Name)
            {
                case "Btn_ok": UpdateEditedPerson();
                    client.Update(EditingPerson);
                    break;
                case "Btn_cancel" :
                default: break;
            }
            EditPanel.Visibility = System.Windows.Visibility.Hidden;
            Btn_Load_Click(this, new RoutedEventArgs());
        }

        private void FillEditPanel()
        {
            Textbox_id.Text = EditingPerson.Id.ToString();
            Textbox_fname.Text = EditingPerson.FName;
            Textbox_lname.Text = EditingPerson.LName;
            Textbox_age.Text = EditingPerson.Age.ToString();
            TextBox_memo.Text = Encoding.UTF8.GetString(EditingPerson.Memo);
            Datagrid_adress.ItemsSource = EditingPerson.Adresses;
            Datagrid_contats.ItemsSource = EditingPerson.Contacts;
            MemoryStream ms = new MemoryStream(EditingPerson.Image);
            BitmapImage bmp = new BitmapImage();
            bmp.BeginInit();
            bmp.StreamSource = ms;
            bmp.EndInit();
            Image_Person.Source = bmp;
        }

        private void UpdateEditedPerson()
        {
            EditingPerson.Id = Convert.ToInt32(Textbox_id.Text);
            EditingPerson.FName = Textbox_fname.Text;
            EditingPerson.LName = Textbox_lname.Text;
            EditingPerson.Age = Convert.ToInt32(Textbox_age.Text);
            EditingPerson.Memo = Encoding.UTF8.GetBytes(TextBox_memo.Text);            
        }

        private void Btn_upload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog OFDialog = new OpenFileDialog();
            OFDialog.Filter = "Image Files | *.bmp; *.gif; *.tif; *.jpg; *.png; ";
            if (OFDialog.ShowDialog().Value)
            {                
                EditingPerson.Image = File.ReadAllBytes(OFDialog.FileName);
                ImageSource imageSource = new BitmapImage(new Uri(OFDialog.FileName));
                Image_Person.Source = imageSource;
            }
            
        }
    }
}
